package com.homework.action;

import java.io.Serializable;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.homework.service.UserService;

public class LoginSuccessAction implements Serializable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -1150387882274164023L;
	private String username;
	private String userId;
	private String roleId;
	UserService userService = new UserService();
	
	public String execute(){
		userId = String.valueOf(userService.findUserIdByUsername(username));
		roleId = String.valueOf(userService.findRoleIdByUsername(username));
		//修改数据库 更新登录时间
		userService.updateLoginTime(Integer.parseInt(userId));
		//修改数据库   更新登陆ip
		userService.updateLoginIp(Integer.parseInt(userId));
		//更改用户状态
		userService.updateUserStatus(userId,4);
		
		// 写入session
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		String userIdTemp = getUserId();
		String roleIdTemp = getRoleId();
		String usernameTemp = getUsername();
		session.setAttribute("userId",userIdTemp);
		session.setAttribute("roleId", roleIdTemp);
		session.setAttribute("username", usernameTemp);
		
		return "Success";
	}


	public String getUserId() {
		return userId;
	}


	public void setUserId(String userId) {
		this.userId = userId;
	}


	public String getRoleId() {
		return roleId;
	}


	public void setRoleId(String roleId) {
		this.roleId = roleId;
	}


	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}
}
